package com.yanqu.road.server.cmd.activity.daomu.crosstogame;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DaoMuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.activity.condition.daomu.DaoMuPvpExpArgs;
import com.yanqu.road.server.logic.activity.condition.daomu.DaoMuPvpPersonRankArgs;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.S_DAOMU_CONDITION_VALUE_CHANGE_FROM_CROSS,desc = "跨服返回-通用条件变更")
public class DaoMuConditionValueChangeCmdFromCross extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DaoMuProto.DaoMuNotifyConditionRespMsg reqMsg = DaoMuProto.DaoMuNotifyConditionRespMsg.parseFrom(packet.getBytes());
        //通知变更
        if (player != null) {
            if (reqMsg.getConditionType() == eGamePlayerEventType.DaoMuPvpExp.getValue()) {
                player.notifyListener(reqMsg.getConditionType(), new DaoMuPvpExpArgs(Integer.parseInt(reqMsg.getParam()), reqMsg.getValue()));
            } else if (reqMsg.getConditionType() == eGamePlayerEventType.DaoMuPvpPersonRank.getValue()) {
                player.notifyListener(reqMsg.getConditionType(), new DaoMuPvpPersonRankArgs(reqMsg.getValue(), reqMsg.getParam()));
            } else {
                player.notifyListener(reqMsg.getConditionType(), reqMsg.getValue());
            }
        } else {
            List<UserActivityConditionData> dataList = UserActivityBussiness.getUserActivityConditionDataListByUserIdAndType(reqMsg.getActivityId(), reqMsg.getUserId(), reqMsg.getConditionType());
            for (UserActivityConditionData conditionData : dataList) {
                if (reqMsg.getConditionType() == eGamePlayerEventType.DaoMuPvpExp.getValue()) {
                    List<ActivityConditionInfo> expConditionInfoList = NormalActivityMgr.getActivityConditionInfoListByType(reqMsg.getActivityId(), eGamePlayerEventType.DaoMuPvpExp.getValue());
                    for (ActivityConditionInfo conditionInfo : expConditionInfoList) {
                        // 需要判断下轮次
                        if (conditionInfo.getConditionId() == conditionData.getConditionId() && conditionInfo.getParamList().get(0).intValue() == Integer.parseInt(reqMsg.getParam())) {
                            conditionData.setValue(BigInteger.valueOf(reqMsg.getValue()));
                        }
                    }
                } else {
                    conditionData.setValue(BigInteger.valueOf(reqMsg.getValue()));
                    conditionData.setParam(reqMsg.getParam());
                }
                // 入库
                UserActivityBussiness.updateUserActivityConditionData(conditionData);
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
